package edu.zut.cs.software.person.staff.service.Impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.person.staff.domain.Staff;
import edu.zut.cs.software.person.staff.service.StaffManager;
import edu.zut.cs.software.teach.Base.service.GenericGenerator;

/**
 * 
 * @author Corner
 *
 */
public class StaffEntityGenerator extends GenericGenerator {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogManager.getLogger(StaffEntityGenerator.class.getName());

	List<Staff> staffs;
	StaffManager staffmanager;

	@Autowired
	public void setStaffmanager(StaffManager staffmanager) {
		this.staffmanager = staffmanager;
	}

	@Before
	public void setUp() throws Exception {
		this.staffs = new ArrayList<Staff>();
		InputStream input = StaffManagerImplTest.class.getResourceAsStream("/sms_staff_category.xlsx");// read excel
																										// file
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0); // 取第一个子表
		DataFormatter dataFormatter = new DataFormatter(); // 格式化存储在单元格中的值
		for (int i = 1; i <= sheet.getLastRowNum(); i++) { // 获取当前工作表的最后一行的行号
			Row row = sheet.getRow(i); // 返回当前行的行号
			Staff s = new Staff();
			for (int j = 1; j <= row.getLastCellNum(); j++) { // 获取列数
				Cell cell = row.getCell(j); // 获取当前表格中的内容
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 1)
						s.setSalary(Integer.parseInt(value));
					if (j == 2)
						s.setName(value);
					if (j == 3)
						s.setWorkgroup(value);
				}
				this.staffs.add(s);
			}
		}
	}

	@Test
	public void test() {
		this.staffmanager.deleteAll();
		this.staffmanager.save(this.staffs);
		Iterable<Staff> result = this.staffmanager.findAll();
		if (logger.isInfoEnabled()) {
			logger.info("test() - Iterable<Staff> result={}", result); //$NON-NLS-1$
		}
	}
}
